﻿<!--/* 
* Copyright (c) 2009, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/-->
    
<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:julmar="http://www.julmar.com/wpfhelpers"
    xmlns:Views="clr-namespace:Bio.Views.Alignment.Views"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    xmlns:Models="clr-namespace:Bio.Views.Alignment.Models"
    xmlns:Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" x:Class="Bio.Views.Alignment.Views.AlignmentEditor"
    FontFamily="Arial" FontSize="12" FocusManager.FocusedElement="{julmar:FirstFocusedElement}"
    MinHeight="400" MinWidth="600" Background="{x:Null}">

    <DockPanel>

        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="VisualStateGroup">
                <VisualStateGroup.Transitions>
                    <VisualTransition From="Normal" GeneratedDuration="00:00:00.1" To="Active"/>
                    <VisualTransition From="Active" GeneratedDuration="00:00:00.1" To="Normal"/>
                </VisualStateGroup.Transitions>
                <VisualState x:Name="Normal">
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="optionPanel" Storyboard.TargetProperty="(FrameworkElement.Height)" To="5" Duration="00:00:00.001" />
                        <DoubleAnimation Storyboard.TargetName="optionPanel" Storyboard.TargetProperty="Opacity" To=".25" Duration="00:00:00.001" />
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Active">
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="optionPanel" Storyboard.TargetProperty="(FrameworkElement.Height)" To="50" Duration="00:00:00.001" />
                        <DoubleAnimation Storyboard.TargetName="optionPanel" Storyboard.TargetProperty="Opacity" To="1" Duration="00:00:00.001" />
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

        <ToolBar x:Name="optionPanel" DockPanel.Dock="Top" Height="5" Opacity=".25" Margin="0,0,40,0">
            <ToolBar.Resources>
                <ObjectDataProvider x:Key="GroupStyles" MethodName="GetValues" ObjectType="{x:Type System:Enum}" >
                    <ObjectDataProvider.MethodParameters>
                        <x:Type TypeName="Models:GroupCollapseStyle" />
                    </ObjectDataProvider.MethodParameters>
                </ObjectDataProvider>
                
                <julmar:DoubleToIntegerConverter x:Key="dbl2IntCvt" />
                <julmar:DeferredBinder x:Key="sliderValue" 
                                       Target="{Binding MinMaxGroupingRange, Converter={StaticResource dbl2IntCvt}, Mode=OneWayToSource}"
                                       Timeout=".5" />
                
                <Style x:Key="RadioButtonList" TargetType="{x:Type ListBox}">
                    <Setter Property="BorderBrush" Value="{x:Null}" />
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="BorderThickness" Value="0" />
                    <Setter Property="ItemsPanel">
                        <Setter.Value>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Vertical" />
                            </ItemsPanelTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="ItemContainerStyle">
                        <Setter.Value>
                            <Style TargetType="{x:Type ListBoxItem}">
                                <Setter Property="Margin" Value="5,0" />
                                <Setter Property="Template">
                                    <Setter.Value>          
                                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                            <Border Background="Transparent">
                                                <RadioButton Focusable="False" IsHitTestVisible="False" IsChecked="{TemplateBinding IsSelected}">
                                                    <ContentPresenter />
                                                </RadioButton>
                                            </Border>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ToolBar.Resources>
            
            <!--
            <Border Visibility="{Binding DisplayGroupings, Converter={julmar:BooleanToVisibilityConverter}}">
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                    <ListBox Margin="5,0" SelectedItem="{Binding MinMaxGroupingStyle}"
                         VerticalAlignment="Center" ItemsSource="{Binding Source={StaticResource GroupStyles}}"
                         Style="{StaticResource RadioButtonList}" />
                    <TextBox Margin="5" MinWidth="100" julmar:NumericTextBoxBehavior.IsEnabled="True" Text="{Binding MinMaxGroupingRange}" />
                </StackPanel>
            </Border>
            -->
            
        	<Interactivity:Interaction.Triggers>
        		<Interactivity:EventTrigger EventName="MouseEnter">
        			<ic:GoToStateAction StateName="Active" UseTransitions="True" />
        		</Interactivity:EventTrigger>
        		<Interactivity:EventTrigger EventName="MouseLeave">
        			<ic:GoToStateAction StateName="Normal" UseTransitions="True" />
        		</Interactivity:EventTrigger>
        	</Interactivity:Interaction.Triggers>
            
            <CheckBox Margin="5" Content="Collapse to Start" 
                  ToolTip="Hide starting columns to the beginning nucleotide"
                  IsEnabled="{Binding SupportsHidingSequences}"
                  IsChecked="{Binding HideStartingEmptySequences}" />

            <CheckBox Margin="5" Content="Group By Taxononmy" IsChecked="{Binding DisplayGroupings}" IsEnabled="{Binding SupportsGrouping}" />

            <StackPanel VerticalAlignment="Center">
                <TextBlock HorizontalAlignment="Center" Text="{Binding ElementName=groupSlider, Path=Value, Converter={StaticResource dbl2IntCvt}}" /> 
                <Slider x:Name="groupSlider" IsEnabled="{Binding DisplayGroupings}" 
                        Value="{Binding Source={StaticResource sliderValue}, Path=Source, Mode=OneWayToSource}"
                        Minimum="1" Maximum="100" SmallChange="1" LargeChange="1" Width="100" 
                        TickFrequency="10" TickPlacement="BottomRight" />
            </StackPanel>
            
            <Button Content="Change Reference Colors" Command="{Binding ChangeReferenceColorsCommand}" MinWidth="100" Margin="5" Padding="5" />
            
        </ToolBar>
        
        <StatusBar DockPanel.Dock="Bottom">
            <StatusBarItem HorizontalAlignment="Right">
                <StackPanel Orientation="Horizontal">
                    <Separator Height="15" />
                    <TextBlock Margin="5,1">
                        <!--<TextBlock.Text>
                            <MultiBinding StringFormat="{}({0},{1})" FallbackValue="(n/a)" >
                                <Binding Path="SelectedCell.Column" />
                                <Binding Path="SelectedCell.Row" />
                            </MultiBinding>
                        </TextBlock.Text>-->
                    </TextBlock>
                </StackPanel> 
            </StatusBarItem>
        </StatusBar>
        
        <Grid x:Name="LayoutRoot">
            <Grid.Resources>
                <PathGeometry x:Key="Nucleotide_A" Figures="M3.8320313,5.5679684 C3.7929688,5.8062496 3.7480469,5.9937496 3.6972656,6.1304684 L2.390625,9.7105465 L5.3027344,9.7105465 L3.984375,6.1304684 C3.9414063,6.0132809 3.8984375,5.8257809 3.8554688,5.5679684 z M3.3515625,4.5484371 L4.3710938,4.5484371 L7.6054688,12.950781 L6.515625,12.950781 L5.625,10.595312 L2.0625,10.595312 L1.2246094,12.950781 L0.12890621,12.950781 z" />
                <PathGeometry x:Key="Nucleotide_C" Figures="M4.83984375,4.40781211853027C5.63671875,4.40781211853027,6.296875,4.52304649353027,6.8203125,4.75351524353027L6.8203125,5.80234336853027C6.21875,5.46640586853027,5.5546875,5.29843711853027,4.828125,5.29843711853027C3.86328125,5.29843711853027,3.0810546875,5.62070274353027,2.4814453125,6.26523399353027C1.8818359375,6.90976524353027,1.58203125,7.77109336853027,1.58203125,8.84921836853027C1.58203125,9.87265586853027,1.8623046875,10.6880855560303,2.4228515625,11.2955074310303C2.9833984375,11.9029293060303,3.71875,12.2066402435303,4.62890625,12.2066402435303C5.47265625,12.2066402435303,6.203125,12.0191402435303,6.8203125,11.6441402435303L6.8203125,12.5992183685303C6.19921875,12.9273433685303,5.42578125,13.0914058685303,4.5,13.0914058685303C3.3046875,13.0914058685303,2.34765625,12.7066402435303,1.62890625,11.9371089935303C0.91015625,11.1675777435303,0.55078125,10.1578121185303,0.550781309604645,8.90781211853027C0.55078125,7.56406211853027,0.955078125,6.47812461853027,1.763671875,5.64999961853027C2.572265625,4.82187461853027,3.59765625,4.40781211853027,4.83984375,4.40781211853027z" />
                <PathGeometry x:Key="Nucleotide_G" Figures="M4.91015625,4.40781211853027C5.82421875,4.40781211853027,6.591796875,4.55624961853027,7.212890625,4.85312461853027L7.212890625,5.94296836853027C6.533203125,5.51328086853027,5.728515625,5.29843711853027,4.798828125,5.29843711853027C3.857421875,5.29843711853027,3.0859375,5.62265586853027,2.484375,6.27109336853027C1.8828125,6.91953086853027,1.58203125,7.75937461853027,1.58203125,8.79062461853027C1.58203125,9.85312461853027,1.861328125,10.6880855560303,2.419921875,11.2955074310303C2.978515625,11.9029293060303,3.736328125,12.2066402435303,4.693359375,12.2066402435303C5.349609375,12.2066402435303,5.91796875,12.0757808685303,6.3984375,11.8140621185303L6.3984375,9.45859336853027L4.55859375,9.45859336853027L4.55859375,8.56796836853027L7.3828125,8.56796836853027L7.3828125,12.3765621185303C6.5390625,12.8531246185303,5.6015625,13.0914058685303,4.5703125,13.0914058685303C3.37109375,13.0914058685303,2.4013671875,12.7046871185303,1.6611328125,11.9312496185303C0.9208984375,11.1578121185303,0.55078125,10.1343746185303,0.55078125,8.86093711853027C0.55078125,7.56015586853027,0.9619140625,6.49277305603027,1.7841796875,5.65878868103027C2.6064453125,4.82480430603027,3.6484375,4.40781211853027,4.91015625,4.40781211853027z" />
                <PathGeometry x:Key="Nucleotide_U" Figures="M0.996093690395355,4.54843711853027L1.98046875,4.54843711853027L1.98046875,9.62265586853027C1.98046875,11.3453121185303,2.70703125,12.2066402435303,4.16015625,12.2066402435303C5.5625,12.2066402435303,6.263671875,11.3746089935303,6.263671875,9.71054649353027L6.263671875,4.54843711853027L7.248046875,4.54843711853027L7.248046875,9.55234336853027C7.248046875,11.9117183685303,6.18359375,13.0914058685303,4.0546875,13.0914058685303C2.015625,13.0914058685303,0.99609375,11.9566402435303,0.996093690395355,9.68710899353027z" />
                <PathGeometry x:Key="Nucleotide_I" Figures="M0.28125,4.54843711853027L2.90625,4.54843711853027L2.90625,5.39218711853027L2.0859375,5.39218711853027L2.0859375,12.1070308685303L2.90625,12.1070308685303L2.90625,12.9507808685303L0.28125,12.9507808685303L0.28125,12.1070308685303L1.1015625,12.1070308685303L1.1015625,5.39218711853027L0.28125,5.39218711853027z" />
                <PathGeometry x:Key="Nucleotide_None" Figures="M2.87109375,8.55624961853027C3.28515625,8.55624961853027,3.744140625,8.72812461853027,4.248046875,9.07187461853027C4.763671875,9.42343711853027,5.1484375,9.59921836853027,5.40234375,9.59921836853027C5.640625,9.59921836853027,5.83203125,9.50351524353027,5.9765625,9.31210899353027C6.12109375,9.12070274353027,6.197265625,8.86874961853027,6.205078125,8.55624961853027L6.99609375,8.55624961853027C6.96875,9.13437461853027,6.8115234375,9.58164024353027,6.5244140625,9.89804649353027C6.2373046875,10.2144527435303,5.84765625,10.3726558685303,5.35546875,10.3726558685303C4.9296875,10.3726558685303,4.412109375,10.1597652435303,3.802734375,9.73398399353027C3.423828125,9.47226524353027,3.11328125,9.34140586853027,2.87109375,9.34140586853027C2.32421875,9.34140586853027,2.0390625,9.68515586853027,2.015625,10.3726558685303L1.224609375,10.3726558685303C1.244140625,9.81406211853027,1.3994140625,9.37167930603027,1.6904296875,9.04550743103027C1.9814453125,8.71933555603027,2.375,8.55624961853027,2.87109375,8.55624961853027z" />
                <PathGeometry x:Key="Nucleotide_Unknown" Figures="M0.240234375,6.95078086853027L1.359375,6.95078086853027L2.51953125,9.00156211853027C2.60546875,9.15390586853027,2.689453125,9.31015586853027,2.771484375,9.47031211853027L2.794921875,9.47031211853027L4.294921875,6.95078086853027L5.35546875,6.95078086853027L3.33984375,9.98593711853027L5.3203125,12.9507808685303L4.201171875,12.9507808685303L3.0234375,11.0054683685303C2.94921875,10.8843746185303,2.861328125,10.7320308685303,2.759765625,10.5484371185303L2.736328125,10.5484371185303C2.716796875,10.5835933685303,2.625,10.7359371185303,2.4609375,11.0054683685303L1.259765625,12.9507808685303L0.15234375,12.9507808685303L2.197265625,10.0093746185303z" />
                <PathGeometry x:Key="Nucleotide_Missing" Figures="M0,8 L9,8 L9,10 L0,10 z" />

                <ContextMenu x:Key="dgContextMenu">
                    <MenuItem Header="Locked" IsCheckable="True" IsChecked="{Binding IsLocked}" />
                    <MenuItem Header="Make Reference Sequence" IsCheckable="True" IsChecked="{Binding IsReferenceSequence}" />
                </ContextMenu>

                <DataTemplate x:Key="ScrollbarToolTip">
                    <Label Content="{Binding Value, Converter={julmar:DoubleToIntegerConverter}}" Foreground="Black" />
                </DataTemplate>

            </Grid.Resources>

            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200" />
                <ColumnDefinition />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Controls:DataGrid x:Name="dg" Grid.ColumnSpan="2" GridLinesVisibility="None" Margin="0,2,0,0"
                           ItemsSource="{Binding Rows}" EnableColumnVirtualization="False" EnableRowVirtualization="False" 
                           Background="White" SelectionUnit="Cell" AutoGenerateColumns="False"
                           HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectionMode="Extended"
                           HeadersVisibility="Column" BorderBrush="DarkGray" BorderThickness="2"
                           CanUserAddRows="False" CanUserDeleteRows="False" 
                           CanUserReorderColumns="False" CanUserResizeColumns="False" 
                           CanUserResizeRows="False" CanUserSortColumns="False" 
                           ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                           ScrollViewer.VerticalScrollBarVisibility="Hidden">

                <Controls:DataGrid.RowStyle>
                    <Style TargetType="{x:Type Controls:DataGridRow}" BasedOn="{StaticResource {x:Type Controls:DataGridRow}}">
                        <Setter Property="ContextMenu" Value="{x:Null}" />
                        <!--<Setter Property="ContextMenu" Value="{StaticResource dgContextMenu}" />-->
                        <Setter Property="DetailsVisibility" Value="Collapsed" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsLocked}" Value="True">
                                <Setter Property="TextElement.Foreground" Value="Red" />
                                <Setter Property="TextElement.FontWeight" Value="Bold" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsGroupHeader}" Value="True">
                                <Setter Property="DetailsVisibility" Value="Visible" />
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type Controls:DataGridRow}">
                                            <Border x:Name="DGR_Border" SnapsToDevicePixels="True" BorderBrush="{x:Null}" BorderThickness="0">
                                                <Controls:DataGridDetailsPresenter Visibility="{TemplateBinding DetailsVisibility}" />
                                            </Border>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Controls:DataGrid.RowStyle>

                <Controls:DataGrid.RowDetailsTemplate>
                    <DataTemplate>
                        <Grid Background="#FF595959">
                            <TextBlock Margin="1" Text="{Binding Header}" ToolTip="{Binding ToolTip}" FontWeight="Bold" Foreground="White" />
                        </Grid>
                    </DataTemplate>
                </Controls:DataGrid.RowDetailsTemplate>

                <Controls:DataGrid.ColumnHeaderStyle>
                    <Style TargetType="{x:Type Controls:DataGridColumnHeader}">
                        <Setter Property="SnapsToDevicePixels" Value="True" />
                        <Setter Property="Width" Value="20" />
                        <Setter Property="ClipToBounds" Value="False" />
                        <Setter Property="Height" Value="20" />
                        <Setter Property="Cursor" Value="Arrow" />
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type Controls:DataGridColumnHeader}">
                                    <Views:HeaderRulerIndex  x:Name="content" 
                                      Foreground="{TemplateBinding Foreground}" 
                                      DisplayIndex="{TemplateBinding DisplayIndex}"
                                      Column="{Binding DataContext.CurrentColumn, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:DataGrid}}}" />
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="DisplayIndex" Value="0">
                                            <Setter Property="Visibility" TargetName="content" Value="Hidden" />
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Controls:DataGrid.ColumnHeaderStyle>

                <Controls:DataGrid.Resources>
                    
                    <SolidColorBrush Color="Goldenrod"  x:Key="{x:Static SystemColors.HighlightBrushKey}" />
                    <SolidColorBrush Color="LightGray"  x:Key="LockedBrush" />

                    <Style x:Key="TextBlockColumnStyle" TargetType="{x:Type TextBlock}">
                        <Setter Property="TextWrapping" Value="NoWrap" />
                        <Setter Property="TextTrimming" Value="CharacterEllipsis" />
                        <Setter Property="ContextMenu" Value="{StaticResource dgContextMenu}" />
                        <Setter Property="ToolTip" Value="{Binding DataContext.ToolTip, RelativeSource={x:Static RelativeSource.Self}}" />
                    </Style>
                    
                    <Style x:Key="CellNucleotideStyle" TargetType="{x:Type Controls:DataGridCell}" BasedOn="{StaticResource {x:Type Controls:DataGridCell}}">
                        <Setter Property="IsSelected" Value="{Binding Content.DataContext.IsSelected, Mode=TwoWay, RelativeSource={RelativeSource Self}, FallbackValue=False}" />
                    </Style>
                    
                    <Style TargetType="{x:Type Views:NucleotideVisual}">
                        <Setter Property="EffectBrush" Value="{Binding BackgroundBrush}" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ParentRow.IsLocked}" Value="True">
                                <Setter Property="EffectBrush" Value="{StaticResource LockedBrush}" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ParentRow.IsReferenceSequence}" Value="True">
                                <Setter Property="EffectBrush" Value="{Binding ParentRow.BackgroundBrush}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                    
                </Controls:DataGrid.Resources>
            </Controls:DataGrid>

            <ScrollBar x:Name="verticalScrollbar" Grid.Column="2" Margin="0,2,0,0" julmar:ScrollingPreviewService.VerticalScrollingPreviewTemplate="{StaticResource ScrollbarToolTip}"
                   Orientation="Vertical" Minimum="0" Maximum="{Binding AffectedRows}" Value="{Binding CurrentRow, Mode=OneWay}" Scroll="OnVerticalScroll"
                   ViewportSize="{Binding VisibleRows}" SmallChange="1" LargeChange="{Binding VisibleRows}" />
            <ScrollBar x:Name="horizontalScrollbar" Grid.Column="1" Grid.Row="1" julmar:ScrollingPreviewService.HorizontalScrollingPreviewTemplate="{StaticResource ScrollbarToolTip}"
                   Orientation="Horizontal" Minimum="0" Maximum="{Binding AffectedColumns}" Value="{Binding CurrentColumn, Mode=OneWay}" Scroll="OnHorizontalScroll"
                   ViewportSize="{Binding VisibleColumns}" SmallChange="1" LargeChange="{Binding VisibleColumns}" />

        </Grid>

    </DockPanel>

</UserControl>
